﻿//using System;
//using System.ComponentModel.Design;
//using System.Data;
//using System.Windows.Forms;
//using EnvDTE;
//using Microsoft.VisualBasic;
//using Microsoft.VisualBasic.CompilerServices;

//namespace Marmat.Data
//{

//    /// <summary>
///// 设置项目配置信息
///// </summary>
///// <remarks></remarks>
//    public abstract class Configuration
//    {
//        public Configuration(string FileName) : this(FileName, null)
//        {
//        }

//        public Configuration(string FileName, string Password)
//        {
//            this.m_FileName = FileName;
//            this.m_Password = Password;
//        }

//        /// <summary>
//    /// 获取或设置配置文件的文件名(包含后缀)。
//    /// </summary>
//    /// <value></value>
//    /// <returns></returns>
//    /// <remarks></remarks>
//        public string FileName
//        {
//            get
//            {
//                return this.m_FileName;
//            }
//        }

//        private string m_FileName;

//        /// <summary>
//    /// 获取或设置配置文件的访问密码。
//    /// </summary>
//    /// <value></value>
//    /// <returns></returns>
//    /// <remarks></remarks>
//        internal string Password
//        {
//            get
//            {
//                return this.m_Password;
//            }
//        }

//        private string m_Password;

//        public static void SetLocalPath(string FileName)
//        {
//            m_LocalPath = FileName;
//        }

//        public static string LocalPath
//        {
//            get
//            {
//                return m_LocalPath;
//            }
//        }

//        private static string m_LocalPath;

//        public static string ConfigurationConnectionString
//        {
//            get
//            {
//                return Conversions.ToString(Operators.ConcatenateObject("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + LocalPath + ";Persist Security Info=False", Interaction.IIf(Operators.CompareString(m_Configuration.Password, "", false) > 0, ";Jet OLEDB:Database Password=" + m_Configuration.Password, null)));
//            }
//        }

//        public static void SetConfiguration(System.ComponentModel.Component Component, IDesignerHost IDesignerHost)
//        {
//            bool NewBoo = false;
//            string _LocalPath = null;
//            try
//            {
//                Configuration Temporary_Configuration = null;
//                var Assembly = System.Reflection.Assembly.GetEntryAssembly();
//                if (IDesignerHost is object)
//                {
//                    var Type = IDesignerHost.GetType(IDesignerHost.RootComponentClassName);
//                    Assembly = Type.Assembly;
//                }

//                if (Assembly is object)
//                {
//                    foreach (Type Item in Assembly.GetExportedTypes())
//                    {
//                        var BaseType = typeof(Configuration);
//                        if (BaseType.IsAssignableFrom(Item))
//                        {
//                            Temporary_Configuration = (Configuration)Item.Assembly.CreateInstance(Item.FullName, true, System.Reflection.BindingFlags.Default, null, null, null, null);
//                            NewBoo = true;
//                            break;
//                        }
//                    }
//                }

//                if (Temporary_Configuration is object && (m_Configuration is null || !m_Configuration.Equals(Temporary_Configuration)))
//                {
//                    m_Configuration = Temporary_Configuration;
//                }

//                if (m_Configuration is object)
//                {
//                    _LocalPath = m_Configuration.get_GetFilePath(Component);
//                    if (NewBoo == true && Operators.CompareString(_LocalPath, "", false) > 0 && My.MyProject.Computer.FileSystem.FileExists(_LocalPath))
//                    {
//                        m_LocalPath = _LocalPath;
//                        var dt = Vars.Get("Configuration", ((int)ConfigurationEnum.数据服务).ToString());
//                        var SymmetricMethod = new SymmetricMethod();
//                        foreach (DataRow dr in dt.Rows)
//                        {
//                            string Content = dr["Content"].ToString();
//                            string Json = SymmetricMethod.Decrypto(Content);
//                            // Dim Item As Marmat.Data.ConnectionInfo = JSONHelper.Deserialize(Of Marmat.Data.ConnectionInfo)(Json)
//                            // If Connections.Items.ContainsKey(Item.Name) = False Then
//                            // Dim Connection As New Marmat.Data.Connection(Item.ConnectionString, Item.SourceType) With {.Name = Item.Name}
//                            // Connections.Items.Add(Item.Name, Connection)
//                            // End If
//                        }
//                        // dt = Marmat.Data.Vars.Get("Configuration", ConfigurationEnum.文件服务)
//                        // For Each dr As System.Data.DataRow In dt.Rows
//                        // Dim Content As String = dr("Content").ToString
//                        // Dim Json As String = SymmetricMethod.Decrypto(Content)
//                        // Dim Item As Marmat.Data.FileServiceInfo = JSONHelper.Deserialize(Of Marmat.Data.FileServiceInfo)(Json)
//                        // If FileServices.Items.ContainsKey(Item.Name) = False Then
//                        // Dim Connection As New Marmat.Data.FileService(Item)
//                        // FileServices.Items.Add(Item.Name, Connection)
//                        // End If
//                        // Next

//                    }
//                }
//            }
//            catch (Exception ex)
//            {
//                Interaction.MsgBox(ex.Message);
//            }
//        }

//        // Public Shared Sub SetConfiguration(ByVal Component As System.ComponentModel.Component, ByVal IDesignerHost As IDesignerHost)
//        // Dim NewBoo As Boolean = False
//        // Dim _LocalPath As String = Nothing
//        // Try
//        // If m_Configuration Is Nothing Then
//        // Dim Assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetEntryAssembly
//        // If IDesignerHost IsNot Nothing Then
//        // Dim Type As Type = IDesignerHost.GetType(IDesignerHost.RootComponentClassName)
//        // Assembly = Type.Assembly
//        // End If
//        // If Assembly IsNot Nothing Then
//        // For Each Item As Type In Assembly.GetExportedTypes
//        // Dim BaseType As Type = GetType(Marmat.Data.Configuration)
//        // If BaseType.IsAssignableFrom(Item) Then
//        // m_Configuration = Item.Assembly.CreateInstance(Item.Namespace & "." & Item.Name, True, Reflection.BindingFlags.Default, Nothing, Nothing, Nothing, Nothing)
//        // NewBoo = True
//        // Exit For
//        // End If
//        // Next
//        // End If
//        // If m_Configuration IsNot Nothing Then
//        // _LocalPath = m_Configuration.GetFilePath(Component)
//        // If NewBoo = True AndAlso _LocalPath > "" AndAlso My.Computer.FileSystem.FileExists(_LocalPath) Then
//        // m_ConfigurationConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _LocalPath & ";Persist Security Info=False" & IIf(m_Configuration.Password > "", ";Jet OLEDB:Database Password=" & m_Configuration.Password, Nothing)
//        // Using OleDbConnection As New System.Data.OleDb.OleDbConnection(m_ConfigurationConnectionString)
//        // If OleDbConnection.State <> ConnectionState.Open Then OleDbConnection.Open()
//        // '获取数据服务器信息
//        // Using adapter As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [Configuration] WHERE [Key] = '" & ConfigurationEnum.数据服务 & "'", OleDbConnection)
//        // Dim dt As DataTable = New System.Data.DataTable
//        // adapter.Fill(dt)
//        // Dim SymmetricMethod As New SymmetricMethod()
//        // For Each dr As System.Data.DataRow In dt.Rows
//        // Dim Content As String = dr("Content").ToString
//        // Dim Json As String = SymmetricMethod.Decrypto(Content)
//        // Dim Item As Marmat.Data.ConnectionInfo = JSONHelper.Deserialize(Of Marmat.Data.ConnectionInfo)(Json)
//        // If Connections.Items.ContainsKey(Item.Name) = False Then
//        // Dim Connection As New Marmat.Data.Connection(Item.ConnectionString, Item.SourceType) With {.Name = Item.Name}
//        // Connections.Items.Add(Item.Name, Connection)
//        // End If
//        // Next
//        // End Using
//        // If OleDbConnection.State <> ConnectionState.Closed Then OleDbConnection.Close()
//        // End Using
//        // End If
//        // End If
//        // End If
//        // Catch ex As Exception
//        // End Try
//        // End Sub
//        internal static Configuration m_Configuration;

//        public static void Remove()
//        {
//            m_Configuration = null;
//            Connections.m_Items = null;
//        }

//        internal string get_GetFilePath(System.ComponentModel.Component Component)
//        {
//            string _LocalPath = Application.StartupPath;
//            if (Component is object)
//            {
//                try
//                {
//                    _LocalPath = this.GetAssemblyDir(this.get_Project(Component));
//                }
//                catch (Exception ex)
//                {
//                }
//            }

//            return My.MyProject.Computer.FileSystem.CombinePath(_LocalPath, this.FileName);
//        }

//        public Project Project(System.ComponentModel.Component Component)
//        {
//            DTE c = (DTE)Component.Site.GetService(typeof(_DTE));
//            object[] Projects = (object[])c.ActiveSolutionProjects;
//            return (Project)Projects[0];
//        }

//        private string GetAssemblyDir(Project vsProject)
//        {
//            string fullPath = vsProject.Properties.Item("FullPath").ToString();
//            string outputPath = vsProject.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").ToString();
//            string outputDir = System.IO.Path.Combine(fullPath, outputPath);
//            return outputDir;
//        }

//        public enum ConfigurationEnum
//        {
//            数据服务 = 100001,
//            文件服务 = 100002
//        }
//    }
//}